/**
 * v-writer
 * 模拟逐字打印
 */

import { nextTick, type Directive, type DirectiveBinding } from 'vue'

const writer: Directive = {
  mounted(el: any, binding: DirectiveBinding) {
    const text = binding.value
    // console.log(text)
    let index = 0
    let container = el.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
    let content = el.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
    // console.log(container, content)
    // 自动滚动到底部函数
    function scrollToBottom() {
      container.scrollTop = content.offsetHeight - container.clientHeight + 15000
    }
    function type() {
      if (index < text.length) {
        el.textContent += text.charAt(index)
        index++
        scrollToBottom()
        setTimeout(type, 25) // 设置打字速度
      } else {
      }
    }
    type()
  }
}

export default writer
